# FILE: PriceTagFactory.py
# DESC: Factory implementation for PriceTag objects.

from web.factory import EntityFactoryManager
from EntityFactory import EntityFactory
from web.entity.PriceTag import PriceTag
from web.entity.NoneEntity import NoneEntity 
from infra.logging import logger 
from infra.exception.InvalidData import InvalidData


class PriceTagFactory(EntityFactory):

	def __init__(self):
		EntityFactory.__init__(self)
		self.currency_ = NoneEntity() 
		self.amount_ = NoneEntity()


	def validate(self, argv):
		rv = True
		toks = argv.split(" ")
		if not toks[0]:
			self.errMsg_ = "Currency is not specified."
			rv = False
		if not toks[1]:
			self.errMsg_ += "Amount is not specified."
			rv = False
		if rv:
			self.currency_ = toks[0]
			self.amount_ = toks[1]

		log = logger.getLogger()
		try:
			entityFactory = EntityFactoryManager.findFactory(self.currency_)
			self.currency_ = entityFactory.createEntity(self.currency_)
			entityFactory = EntityFactoryManager.findFactory(self.amount_)
			self.amount_ = entityFactory.createEntity(self.amount_)
		except EntityFactoryManager.NoSuchEntityFactory, e:
			log.error("caught exception NoSuchEntityFactory, reason: %s" % e)
			raise
		except InvalidData, e:
			log.error("caught exception InvalidData, reason: %s" % e)
			raise

		return rv 


	def doCreateEntity(self, consumer, relevanceFactor):
		priceTag = PriceTag(self.currency_, self.amount_)

		if consumer:
			consumer.onPriceTag(priceTag)

		return priceTag 


	def getEntityType(self):
		return "^PriceTag$"
